package cn.iocoder.yudao.module.signature.controller.admin.device;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.signature.controller.admin.device.vo.AppleDevicePageReqVO;
import cn.iocoder.yudao.module.signature.controller.admin.device.vo.AppleDeviceRespVO;
import cn.iocoder.yudao.module.signature.controller.admin.device.vo.AppleDeviceSaveReqVO;
import cn.iocoder.yudao.module.signature.dal.dataobject.device.AppleDeviceDO;
import cn.iocoder.yudao.module.signature.model.MobileProvisionCreateModel;
import cn.iocoder.yudao.module.signature.model.MobileProvisionResultModel;
import cn.iocoder.yudao.module.signature.service.device.AppleDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 苹果设备信息")
@RestController
@RequestMapping("/signature/apple-device")
@Validated
public class AppleDeviceController {

    @Resource
    private AppleDeviceService appleDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建苹果设备信息")
    @PreAuthorize("@ss.hasPermission('signature:apple-device:create')")
    public CommonResult<Long> createAppleDevice(@Valid @RequestBody AppleDeviceSaveReqVO createReqVO) {
        return success(appleDeviceService.createAppleDevice(createReqVO));
    }


    @PostMapping("/createMobileProvision")
    @Operation(summary = "创建苹果设备信息")
    public CommonResult<MobileProvisionResultModel> createAppleDevice(@Valid @RequestBody MobileProvisionCreateModel createReqVO) throws Exception {
        return success(appleDeviceService.createMobileProvision(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新苹果设备信息")
    @PreAuthorize("@ss.hasPermission('signature:apple-device:update')")
    public CommonResult<Boolean> updateAppleDevice(@Valid @RequestBody AppleDeviceSaveReqVO updateReqVO) {
        appleDeviceService.updateAppleDevice(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除苹果设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('signature:apple-device:delete')")
    public CommonResult<Boolean> deleteAppleDevice(@RequestBody List<Long> ids) {
        appleDeviceService.deleteAppleDevice(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得苹果设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('signature:apple-device:query')")
    public CommonResult<AppleDeviceRespVO> getAppleDevice(@RequestParam("id") Long id) {
        AppleDeviceDO appleDevice = appleDeviceService.getAppleDevice(id);
        return success(BeanUtils.toBean(appleDevice, AppleDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得苹果设备信息分页")
    @PreAuthorize("@ss.hasPermission('signature:apple-device:query')")
    public CommonResult<PageResult<AppleDeviceRespVO>> getAppleDevicePage(@Valid AppleDevicePageReqVO pageReqVO) {
        PageResult<AppleDeviceDO> pageResult = appleDeviceService.getAppleDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppleDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出苹果设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('signature:apple-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppleDeviceExcel(@Valid AppleDevicePageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppleDeviceDO> list = appleDeviceService.getAppleDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "苹果设备信息.xls", "数据", AppleDeviceRespVO.class,
                BeanUtils.toBean(list, AppleDeviceRespVO.class));
    }

}